package com.domain.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.domain.common.AppLogger;
import com.domain.common.Comments;
import com.domain.common.Const;
import com.domain.common.Result;
import com.domain.dao.ContactsMapper;
import com.domain.dao.UsersAddFriendsMapper;
import com.domain.dao.UsersMapper;
import com.domain.pojo.Contacts;
import com.domain.pojo.Users;
import com.domain.pojo.UsersAddFriends;
import com.domain.service.ContactsService;
import com.domain.utils.NotNullUtils;
import com.domain.utils.UUIDUtils;
import com.domain.utils.wyyx.IMSet;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.*;

@Service
public class ContactsServiceImpl implements ContactsService {

    /**
     * 日志
     */
    private static Logger logger = AppLogger.getLogger(ContactsServiceImpl.class.getSimpleName(), Level.INFO.toString(), true);

    @Autowired
    private ContactsMapper contactsMapper;

    @Autowired
    private UsersMapper usersMapper;

    @Autowired
    private UsersAddFriendsMapper usersAddFriendsMapper;


    /**
     * 同步通讯录
     *
     * @param userId
     * @param jsonStr
     * @return
     */
    @Override
    public Result synchronizationContacts(String userId, String jsonStr) throws IOException {
        logger.info("获取的jsonStr" + jsonStr);
        JSONArray json = JSON.parseArray(jsonStr);
        Iterator it = json.iterator();
        logger.info("迭代的it数据" + it);
        while (it.hasNext()) {
            JSONObject sObj = (JSONObject) it.next();
            String phoneNum = (String) sObj.get("phoneNum");
            String name = (String) sObj.get("name");
            //获取app好友信息
            logger.info("users开始*******************************");
            Users user = usersMapper.selectByUserPhone(phoneNum);
            logger.info("users结束*******************************" + user);
            Contacts contacts = new Contacts();
            if (NotNullUtils.isNotNullObject(user)) {
                //保存app好友
                contacts.setType(Comments.CONTACTS_APP_);
                contacts.setSpareTwo(user.getId());
                contacts.setHeadImage(user.getHeadImage());
                contacts.setName(user.getName());
                //设置为网易云信好友
                IMSet.addFriend(userId, user.getId(), "1", "");
            } else {
                contacts.setType(Comments.CONTACTS);
                contacts.setHeadImage(Const.USER_HEAD_IMAGE);
                contacts.setName(name);
            }
            contacts.setId(UUIDUtils.getRandomUUID());
            contacts.setUserId(userId);
            contacts.setCreatTime(new Date());
            logger.info("contans" + contacts);
            contactsMapper.insertSelective(contacts);
        }
        return Result.success();
    }

    /**
     * 获取通讯录列表()搜索项
     *
     * @param userId   用户id
     * @param phoneNum 电话号码
     * @param name     真实姓名
     * @param pageNum  页码
     * @param pageSize 每页显示的长度
     * @return
     */
    @Override
    public Result getContactsList(String userId, String phoneNum, String name, String pageNum, String pageSize) {
        PageHelper.startPage(new Integer(pageNum), new Integer(pageSize));
        Map<String, Object> map = new HashMap<>();
        if (NotNullUtils.isNotNullObject(name)) {
            map.put("name", name);
        }
        if (NotNullUtils.isNotNullObject(phoneNum)) {
            map.put("phoneNum", phoneNum);
        }
        map.put("userId", userId);
        List<Contacts> contactsList = contactsMapper.selectByUserId(map);
        PageInfo pageInfo = new PageInfo(contactsList);
        return Result.success("操作成功", pageInfo.getList());
    }

    /**
     * 获取加好友请求列表
     *
     * @param userId
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public Result getNewFriendsRequestList(String userId, String pageNum, String pageSize) {
        PageHelper.startPage(new Integer(pageNum), new Integer(pageSize));
        List<Map<String, Object>> friends = usersAddFriendsMapper.selectByUsersId(userId);
        PageInfo pageInfo = new PageInfo(friends);
        return Result.success("操作成功", pageInfo.getList());
    }

    /**
     * 同意加好友
     *
     * @param addFriedndsId 请求好友记录id
     * @param userId        用户id
     * @param friendsId     好友id
     * @return
     */
    @Override
    public Result agreeAddFriends(String addFriedndsId, String userId, String friendsId) throws IOException {
        String res = IMSet.addFriend(userId, friendsId, "3", "");
        //修改请求记录状态
        UsersAddFriends us = new UsersAddFriends();
        us.setId(addFriedndsId);
        us.setType(Const.AGREE_FRIENDS);
        usersAddFriendsMapper.updateByPrimaryKeySelective(us);
        return Result.success("操作成功", res);
    }


}
